home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 25 / Mac Magazin and MacEasy Magazine CD - Issue 25.iso / Online / Joe's-E-mail-Scripts / Speaking E-mail / Speaking E-mail (text) < prev   
Text File  |  1996-08-20  |  6KB  |  188 lines

  1.  
  2. global serverResponse, sss, inBox
  3.  
  4. on run
  5.     
  6.     set closeAfter to false
  7.     if not (PPPopened) then
  8.         set closeAfter to true
  9.         openPPP (getPPPServer)
  10.         repeat while not (PPPopened)
  11.         end repeat
  12.     end if
  13.     
  14.     set sss to (tcp connect to host (GetICPreference SMTP host) port 110)
  15.     try
  16.         readresponse(sss)
  17.         tcp write data "USER " & (GetICPreference mail account) & return stream sss using ISO88591
  18.         readresponse(sss)
  19.         tcp write data "PASS " & (GetICPreference mail password) & return stream sss using ISO88591
  20.         readresponse(sss)
  21.         tcp write data "STAT" & return stream sss using ISO88591
  22.         readresponse(sss)
  23.         set oldDelimiters to AppleScript's text item delimiters
  24.         set AppleScript's text item delimiters to {" "}
  25.         set messageCount to text item 2 of serverResponse as integer
  26.         set AppleScript's text item delimiters to oldDelimiters
  27.         
  28.         if messageCount ≠ 0 then
  29.             if messageCount = 1 then
  30.                 speak "There is one message"
  31.             else
  32.                 speak "There are " & messageCount & " new messages"
  33.             end if
  34.             
  35.             tell application "Finder"
  36.                 if not (folder "AppleScript Inbox" of desktop exists) then
  37.                     set inBox to (make folder with properties {name:"AppleScript Inbox"}) as string
  38.                 else
  39.                     set inBox to (folder "AppleScript Inbox" of desktop) as string
  40.                 end if
  41.             end tell
  42.             
  43.             set currentMessage to 1
  44.             repeat while currentMessage ≤ messageCount
  45.                 
  46.                 getMail(currentMessage)
  47.                 
  48.                 set currentMessage to currentMessage + 1
  49.             end repeat
  50.             
  51.             
  52.         else
  53.             speak "There are no messages"
  54.         end if
  55.         tcp close stream sss
  56.         
  57.     on error msg number num
  58.         tcp close stream sss
  59.         display dialog "Error. Script Aborted." buttons {"OK"} default button "OK"
  60.         quit
  61.     end try
  62.     
  63.     if closeAfter = true then
  64.         if (PPPopened) = true then
  65.             closePPP with hard close
  66.         end if
  67.     end if
  68.     
  69. end run
  70.  
  71. on open (docList)
  72.     set messageCount to (count of docList) as string
  73.     if messageCount ≠ "1" then
  74.         speak messageCount & " messages to be sent"
  75.     else
  76.         speak "one message to be sent"
  77.     end if
  78.     set closeAfter to false
  79.     if not (PPPopened) then
  80.         set closeAfter to true
  81.         openPPP (getPPPServer)
  82.         repeat while not (PPPopened)
  83.         end repeat
  84.     end if
  85.     
  86.     set LF to ASCII character (10)
  87.     set CR to return
  88.     set CRLF to CR & LF
  89.     
  90.     set sss to (tcp connect to host (GetICPreference SMTP host) port 25)
  91.     readresponse(sss)
  92.     tcp write data "HELO " & (tcp my address) & return stream sss using ISO88591
  93.     readresponse(sss)
  94.     try
  95.         
  96.         repeat with aFile in docList
  97.             set aFile to contents of aFile
  98.             tcp write data "mail from: " & (GetICPreference mail account) & return ¬
  99.                 stream sss using ISO88591
  100.             readresponse(sss)
  101.             
  102.             set afilename to aFile as string
  103.             set oldDelimiters to AppleScript's text item delimiters
  104.             set AppleScript's text item delimiters to {":"}
  105.             set theFileName to last text item of afilename
  106.             set AppleScript's text item delimiters to oldDelimiters
  107.             set emailSubject to theFileName as text
  108.             open for access afilename
  109.             set sendToaddress to (read afilename before return)
  110.             set mailContents to (read afilename)
  111.             close access afilename
  112.             
  113.             tcp write data "rcpt to: " & sendToaddress & return ¬
  114.                 stream sss using ISO88591
  115.             readresponse(sss)
  116.             tcp write data "data" & return stream sss using ISO88591
  117.             readresponse(sss)
  118.             tcp write data "To: " & sendToaddress & return stream sss using ISO88591
  119.             tcp write data "Subject: " & emailSubject & return stream sss using ISO88591
  120.             tcp write data mailContents & return stream sss using ISO88591
  121.             tcp write data "." & return stream sss using ISO88591
  122.             readresponse(sss)
  123.         end repeat
  124.         
  125.         tcp close stream sss
  126.         speak "transmission completed"
  127.         if closeAfter = true then
  128.             if (PPPopened) = true then
  129.                 closePPP with hard close
  130.             end if
  131.         end if
  132.         return
  133.     on error msg number num
  134.         tcp close stream sss
  135.         display dialog "Error. Script Aborted." buttons {"OK"} default button "OK"
  136.         quit
  137.     end try
  138.     
  139. end open
  140.  
  141. on getMail(currentMessage)
  142.     set LF to ASCII character (10)
  143.     set nowTotal to (time string of (current date))
  144.     set oldDelimiters to AppleScript's text item delimiters
  145.     set AppleScript's text item delimiters to {":"}
  146.     set nowHour to text item 1 of nowTotal
  147.     set nowMin to text item 2 of nowTotal
  148.     set nowSec to text item 3 of nowTotal
  149.     set nowString to nowHour & "h" & nowMin & "m" & nowSec & "s"
  150.     set AppleScript's text item delimiters to oldDelimiters
  151.     set currentFilename to inBox & "Message #" & currentMessage & " " & nowString
  152.     open for access file currentFilename with write permission
  153.     tcp write data "RETR " & currentMessage & return stream sss using ISO88591
  154.     readresponse(sss)
  155.     set oldDelimiters to AppleScript's text item delimiters
  156.     set AppleScript's text item delimiters to {" "}
  157.     set messageSize to text item 2 of serverResponse as integer
  158.     set AppleScript's text item delimiters to oldDelimiters
  159.     
  160.     repeat until (bytes waiting of (tcp status stream sss) = 0)
  161.         
  162.         write (tcp read stream sss using ISO88591) to file currentFilename starting at eof
  163.         
  164.         set beginWait to time of (current date)
  165.         repeat until time of (current date) > beginWait + 1
  166.         end repeat
  167.     end repeat
  168.     
  169.     if (get eof currentFilename) > messageSize * 0.75 then
  170.         tcp write data "DELE " & currentMessage & return stream sss using ISO88591
  171.         readresponse(sss)
  172.     end if
  173.     close access file currentFilename
  174.     
  175. end getMail
  176.  
  177. on readresponse(sstream)
  178.     set LF to ASCII character (10)
  179.     repeat until (tcp ahead characters LF stream sstream)
  180.     end repeat
  181.     set serverResponse to (tcp read until characters LF stream sstream using ISO88591)
  182. end readresponse
  183.  
  184. on quit
  185.     continue quit
  186. end quit
  187.  
  188.